// pages/selection/selection.js
import {
    areaList
} from '~/miniprogram_npm/@vant/area-data/index';
import baseuser from '~/utils/users.js'
import baseteam from '~/utils/teamleader.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        areaList,
        active1: '全部',
        option1: [],
        shew1: false,
        shew2: false,
        shew3: false,
        textvalue1: '',
        textvalue2: '',
        textvalue3: '',
        textvalue4: '',
        changeareadata: [],

        active2: 0,
        option2: [
            '跟团多',
            '销量多',
            '佣金高',
            '浏览量高',
            '有头部团长带货',
            '可授权',
            '运费险',
            '正品保障',
        ],
        equity: [
            '有头部团长带货',
            '可授权',
            '运费险',
            '正品保障'
        ],
        listCon: {
            category: '',
            startingPrice: 'desc',
            brokerage: "desc",
            shipAddress: "",
            safeguard: "",
            price: "",
            commission: "",
            current: 1,
            pageSize: 10
        },
        updateListCon: null,

        indexlist: [],

        pages: 1,
        startingPrice: 0,
        brokerage: 0,
        selectedClass: '', // 初始类名为 'selection_main_labelmain_select'  
        equity1: 0,
        safeguard: '',
        synthesis: 1,
        priceSelect: 0, //价格列表选择
        commissionSelect: 0, //佣金率列表选择
        optionPrice: [], //价格列表
        optionCommission: [{
                text: '0~20',
                valve: [0, 20]
            }

        ], //佣金率列表
    },
    resetting() {

    },
    // 筛选价格（最低）
    bottomPrice(e) {
        this.setData({
            textvalue1: e.target.dataset.text
        })
    },
    topPrice(e) {
        this.setData({
            textvalue2: e.target.dataset.text
        })
    },
    lowestCommission(e) {
        this.setData({
            textvalue3: e.target.dataset.text
        })
    },
    topCommission(e) {

        this.setData({
            textvalue4: e.target.dataset.text
        })
    },
    complete() {
      this.complete()
        this.setData({
            'updateListCon.price': this.data.textvalue1 + '~' + this.data.textvalue2,
            'updateListCon.commission': this.data.textvalue3 + '~' + this.data.textvalue4,
            shew2: false

        })
        console.log(this.data.textvalue1 + '~' + this.data.textvalue2);
        this.gatallcomp()

    },
    tabsonChange(items) {
        this.setData({
            active1: items.detail.title,
        })
        console.log('items', items)

        this.gatallcomp(this.data.active1)
    },

    gotoselectionLibrarySearch() {
        wx.navigateTo({
            url: `/search/selectionLibrarySearch/selectionLibrarySearch`,
        })
    },
    nextdetail(e) {
        wx.navigateTo({
            url: `../details/details?id=${e.currentTarget.dataset.id}`,
        })
    },
    guarantee(e) {
        console.log('e', e.target.dataset.text, );

        this.setData({
            'updateListCon.safeguard': e.target.dataset.text,
            'equity1': e.currentTarget.dataset.select
        })

    },
    locations() {
        this.setData({
            shew3: true
        });
    },
    onClose3() {
        this.setData({
            shew3: false
        });
    },
    confirm3(item) {
        this.setData({
            changeareadata: item.detail.values
        });
        console.log('changeareadata', this.data.changeareadata);
        this.onClose3()
    },
    onclia() {
        console.log('1')
    },
    onClose2() {
        this.setData({
            shew2: false
        });
    },
    onClose() {
        this.setData({
            shew1: false
        });
    },

    onSelect(event) {
        console.log(event.detail);
    },
    openq() {
        this.setData({
            shew2: true,
            synthesis: 0

        })
    },
    naveback() {
        wx.navigateBack()
    },
    selectchanges(item) {
        console.log(item.currentTarget.dataset.changes)
        this.setData({
            baostart: item.currentTarget.dataset.changes
        })
    },
  
    async getfenlei() {
        // let resdata = await baseuser.profirst()
        // let newdata = resdata.data.result
        // this.setData({
        //     option1: newdata
        // })
        let resdata = await baseteam.zhuyinglist()
        // console.log(resdata)
        // let newdata = resdata.data.result
        // for (const i in newdata) {
        //     newdata[i].text = newdata[i].productName
        //     newdata[i].value = newdata[i].productName
        // }
        this.setData({
            option1: resdata.data.result
        })

        this.selectComponent('#vatber').resize();
    },
    async startingPricechange() {
        console.log('开团价格', this.data.updateListCon)
        this.setData({
            synthesis: 0

        })
        if (this.data.updateListCon.startingPrice == 'desc') {
            // console.log(111)
            let resdata = await baseuser.SelectionLibrary(this.data.updateListCon)
            console.log('开团价格', resdata);

            if (resdata.code == 200) {
                this.setData({
                    indexlist: resdata.data.result.records,
                    'updateListCon.startingPrice': 'asc',
                    startingPrice: 2
                })
            } else {
                wx.showToast({
                    title: '查询失败',
                    icon: 'none',
                    duration: 2000
                })
            }
            // console.log("indexlist", this.data.indexlist);
            // console.log('更改后',this.data.updateListCon)
        } else if (this.data.updateListCon.startingPrice == 'asc') {
            // console.log(222)
            let resdata = await baseuser.SelectionLibrary(this.data.updateListCon)
            if (resdata.code == 200) {
                this.setData({
                    indexlist: resdata.data.result.records,
                    'updateListCon.startingPrice': 'desc',
                    startingPrice: 1
                })
            } else {
                wx.showToast({
                    title: '查询失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        }
    },
    async brokeragechange() {
        this.setData({
            synthesis: 0

        })
        if (this.data.updateListCon.brokerage == 'desc') {
            console.log(111)
            let resdata = await baseuser.SelectionLibrary(this.data.updateListCon)
            if (resdata.code == 200) {
                this.setData({
                    indexlist: resdata.data.result.records,
                    'updateListCon.brokerage': 'asc',
                    brokerage: 2
                })
            } else {
                wx.showToast({
                    title: '查询失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        } else if (this.data.updateListCon.brokerage == 'asc') {
            let resdata = await baseuser.SelectionLibrary(this.data.updateListCon)
            console.log('resdata', resdata);
            if (resdata.code == 200) {
                this.setData({
                    indexlist: resdata.data.result.records,
                    'updateListCon.brokerage': 'desc',
                    brokerage: 1
                })

            } else {
                wx.showToast({
                    title: '查询失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        }
    },
    async zongclone() {
        // this.setData({
        //   'updateListCon.startingPrice': 'desc',
        //   'updateListCon.brokerage': 'desc',
        // })
        this.setData({
            synthesis: 1

        })
        console.log('this.data.updateListCon', this.data.updateListCon)
        // this.gatallcomp()
        let resdata = await baseuser.SelectionLibrary(this.data.updateListCon)
        if (resdata.code == 200) {
            this.setData({
                indexlist: resdata.data.result.records
            })
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
        }
        console.log('综合', resdata);
    },
    /*重置筛选条件*/
    reset() {
        console.log('213')
        this.setData({
            commissionSelect:-1,
            priceSelect: -1,
            address: '',
            equity1: -1,
            textvalue1: '',
            textvalue2: '',
            textvalue3: '',
            textvalue4: '',
        })
    },

    /*获取价格*/
    async getPriceList() {
        let datas = await baseuser.priceList()
        let temp = datas.data.Result
        let newData = []
        for (const i in temp) {
            newData[i] = {}
            newData[i].text = temp[i].minPrice + '~' + temp[i].maxPrice
            newData[i].value = []
            newData[i].value[0] = temp[i].minPrice
            newData[i].value[1] = temp[i].maxPrice
        }
        this.setData({
            textvalue1: temp[0].minPrice,
            textvalue2: temp[0].maxPrice,
            optionPrice: newData
        })
    },
    /*获取佣金率*/
    async getCommissionList(){
        let datas = await baseuser.brokerage()
        let temp = datas.data.Result
        let newData = []
        for (const i in temp) {
            newData[i] = {}
            newData[i].text = temp[i].minBrokerage + '~' + temp[i].maxBrokerage
            newData[i].value = []
            newData[i].value[0] = temp[i].minBrokerage
            newData[i].value[1] = temp[i].maxBrokerage
        }
        this.setData({
            textvalue3: temp[0].minBrokerage,
            textvalue4: temp[0].maxBrokerage,
            optionCommission: newData
        })
    },
    /*点击价格显示在输入框*/
    checkPrice(e) {
        this.setData({
            priceSelect: e.target.dataset.select,
            textvalue1: e.target.dataset.item.value[0],
            textvalue2: e.target.dataset.item.value[1]
        })
    },
    /*点击佣金率显示在输入框*/
    checkCommission(e) {
        this.setData({
            commissionSelect: e.target.dataset.select,
            textvalue3: e.target.dataset.item.value[0],
            textvalue4: e.target.dataset.item.value[1]
        })
    },
    scrollbind(items) {
        console.log('actt', items.currentTarget)
        this.setData({
            // "listCon.category": '全部',
            active2: items.currentTarget.dataset.select,
            // "listCon.safeguard":items.currentTarget.dataset.text

            listCon: {
                ...location,
                category: '全部',
                safeguard: items.currentTarget.dataset.text,

                startingPrice: 'desc',
                brokerage: "desc",
                shipAddress: "",

                price: "",
                commission: "",
                current: 1,
                pageSize: 10
            }
        })
        console.log('全部', this.data.listCon);
        this.gatallcomp()

    },
    async gatallcomp() {
        // console.log('category', category)


        this.setData({
            "listCon.category": this.data.active1,
        })

        this.data.updateListCon = this.data.listCon
        console.log('listcon', this.data.listCon)
        let resdata = await baseuser.SelectionLibrary(this.data.listCon)
        console.log('resdata888', resdata)
        if (resdata.code == 200) {
            this.setData({
                indexlist: resdata.data.result.records
            })
        } else {
            wx.showToast({
                title: '查询失败',
                icon: 'none',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getfenlei()
        let Bartop = (0, wx.getMenuButtonBoundingClientRect)().top;
        let BarHeight = (0, wx.getMenuButtonBoundingClientRect)().height;

        console.log(wx.getMenuButtonBoundingClientRect())
        this.setData({
            settop: Bartop + BarHeight * 1.5
        })
        this.getPriceList()
        this.getCommissionList()
        this.gatallcomp()
        let items = {
            detail: {
                title: '全部'
            }
        }; // 这里只是一个示例，你应该根据实际情况定义items  
        this.tabsonChange(items);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})